package com.urbanski.clipboard.server.action.message;

import java.io.IOException;
import org.apache.log4j.Logger;
import com.urbanski.clipboard.common.message.CreateMessage;
import com.urbanski.clipboard.common.message.CreateResponse;
import com.urbanski.clipboard.common.message.Message;
import com.urbanski.clipboard.server.ClientSocket;
import com.urbanski.clipboard.server.ClipboardServer;
import com.urbanski.clipboard.server.session.ServerSession;
import com.urbanski.util.logging.LoggingUtil;

public class CreateMessageHandler extends ServerMessageHandler
{
	private static final Logger LOGGER = Logger.getLogger(CreateMessageHandler.class);

	public CreateMessageHandler(ClientSocket client)
	{
		super(client);
	}

	@Override
	public void handle(Message message)
	{
		CreateMessage createMessage = (CreateMessage) message;
		
		ServerSession session = new ServerSession(createMessage.getSessionName(), createMessage.getAdminPassword(), createMessage.getSessionPassword());
		session.addClient(client);
		
		CreateResponse response = ClipboardServer.createSession(session);
		
		System.out.println("Session create response: " + response.getResponseText());
		
		try
		{
			client.sendResponse(response);
		}
		catch (IOException e)
		{
			LoggingUtil.logAndPrintError(LOGGER, "Error sending response", e);
		}
	}

}
